package com.hsnn.medstgmini.pro.base.dao.Impl;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.base.dict.dao.impl.BaseHibernateDAO;
import com.hsnn.medstgmini.pro.base.dao.BaseProduceDao;
import com.hsnn.medstgmini.util.Pagination;

@Repository
public class BaseProduceDaoImpl extends BaseHibernateDAO implements BaseProduceDao{

	/**
	 * @category 查询配送企业目录信息
	 * @author 单大伟
	 * @date   2015年6月15日
	 * @param  @param viewGoods
	 * @param  @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getViewGoodsList(Pagination page) {
		String projId = page.getConditions().get("projId");
		String compName = page.getConditions().get("compName");
		String compId = page.getConditions().get("compId");
		String compCode = page.getConditions().get("compCode");
		
		StringBuffer projIdCond = new StringBuffer();
		if (StringUtils.isBlank(projId)) {
			StringBuffer projIdSql = new StringBuffer();
			projIdSql.append("SELECT distinct bp.proj_id FROM bid_project bp LEFT JOIN bid_projdrugs bpd ON bp.proj_id = bpd.proj_id ")
			         .append(" INNER JOIN std_druginfo sdi ON bpd.drug_id = sdi.drug_id where bp.status = 1 ")
			         .append(" and bp.starttime <= SYSDATE() and bp.endtime >= SYSDATE() and bpd.status=1 and sdi.status=1 ")
			         .append(" and sdi.prod_comp_id='").append(compId).append("' ");
			Query query = this.getSession().createSQLQuery(projIdSql.toString());
			List<String> projIdList = query.list();
			
			projIdCond.append("('");
			for (int i = 0; i < projIdList.size(); ++i) {
				projIdCond.append(projIdList.get(i)).append("'");
				if (i != projIdList.size() - 1) {
					projIdCond.append(",'");
				}
			}
			projIdCond.append(")");
		} else {
			projIdCond.append("('").append(projId).append("')");
		}
		
		StringBuffer hql = new StringBuffer();
		StringBuffer hqlCount = new StringBuffer("select count(*) ");
		StringBuffer hqlEnd = new StringBuffer();
		
		hql.append("select sci.comp_code as compCode, sci.comp_name as compName, sci.org_code as orgCode, ")
		   .append("(case when scg.comp_group_name is null then '--' else scg.comp_group_name end) as compGroupName, ")
		   .append("da.ar_fullname as arFullName, sci.comp_phone as compPhone");

        hqlEnd.append(" from bid_projcompany bpj left join std_compinfo sci on bpj.comp_id = sci.comp_id ")
		   .append(" left join std_compgroup scg on sci.comp_group_id = scg.comp_group_id left join dic_area da ")
		   .append(" on sci.area_id=da.area_id where 1=1 ");
		
		if (StringUtils.isNotBlank(compName)) {
			hqlEnd.append(" and sci.comp_name like :CPN ");
		}
		  
		if (StringUtils.isNotBlank(compCode)) {
			hqlEnd.append(" and sci.comp_code = :compCode ");
		}
		
	    hqlEnd.append(" and bpj.proj_id in ")
		       .append(projIdCond)
		       .append(" group by sci.comp_id");
	    String str = hql.append(hqlEnd).toString();
	    String strCount = hqlCount.append(hqlEnd).toString();
	    
	    Query queryData = this.getSession().createSQLQuery(str);
	    Query queryCount = this.getSession().createSQLQuery(strCount);
		
		if (StringUtils.isNotBlank(compName)) {
			queryData.setParameter("CPN", "%" + compName + "%");
			queryCount.setParameter("CPN", "%" + compName + "%");
		}
		
		if (StringUtils.isNotBlank(compCode)) {
			queryData.setParameter("compCode", compCode);
			queryCount.setParameter("compCode", compCode);
		}
		
		Long count = ((BigInteger) queryCount.list().get(0)).longValue();
		
		queryData.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		queryData.setFirstResult((page.getPage() - 1) * page.getMaxResults());
		queryData.setMaxResults(page.getMaxResults());
		
		List<Map> list = queryData.list();

		if (list.size() > 0) {
			page.setRows(list);
		} else {
			page.setRows(0);
		}
		
		page.setRecords(count);
        page.setTotal(new Long(count % page.getMaxResults() == 0 ? count / page.getMaxResults() : count / page.getMaxResults() + 1).intValue());
		
		return page;
	}

}